package com.noble.meether.mail.ui.friend;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;

import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.bean.center.user.light.UserLightList;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.view.custom.state.CustomStatusListView;
import com.noble.library.widget.ListView.xlistview.ExListView;
import com.noble.meether.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 黑名单列表
 */
public class MyDefriendAct extends BaseAppCompatActivity implements RequestComplete, ExListView.IXListViewListener {
    private CustomStatusListView customStatusListView;
    private ExListView exListView;

    private MyDefriendAdapter adapter;
    private List<UserLight> data = new ArrayList<>();

    private int page = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_friends);
        initTitle();
        initView();
        onRefresh();
    }

    private void initTitle() {
        setBackView();
        setTitle(getResources().getString(R.string.my_defriend_title));
    }

    private void initView() {
        customStatusListView = findViewById(R.id.myfriend_list);
        View mViewTop = LayoutInflater.from(this).inflate(R.layout.layout_margintop, null);
        exListView = customStatusListView.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(false);
        exListView.addHeaderView(mViewTop);
        adapter = new MyDefriendAdapter(this, data);
        exListView.setAdapter(adapter);

        customStatusListView.showLoading();
    }

    @Override
    public void onRefresh() {
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getCommonMgr().getMyDefriends(page, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getCommonMgr().getMyDefriends(page, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.isOk()) {
            UserLightList infoList = (UserLightList) response.getBaseData();
            if (infoList != null && infoList.getLightList().size() != 0) {
                if (data.size() != 0) {
                    data.clear();
                }
                data.addAll(infoList.getLightList());
                if (data.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                customStatusListView.showExListView();
            } else {
                customStatusListView.showNoData(getString(R.string.my_defriend_nodata));
            }
            if (page == 1) {
                exListView.stopRefresh();
            } else {
                exListView.stopLoadMore();
            }
        } else {
            customStatusListView.showNoData("请求出错", "重试", view -> {
                customStatusListView.showLoading();
                onRefresh();
            });
        }
    }
}